#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:xaoyaoyao
@file: rotate_proxy.py
@time: 2018/08/19
"""

import random
import logging
from article_spider.settings import PROXY_IPS


class ProxyMiddleWare(object):
    """docstring for ProxyMiddleWare"""

    def process_request(self, request, spider):
        '''对request对象加上proxy'''
        proxy = self.get_random_proxy()
        logging.info("this is request ip >> %s", proxy)
        request.meta['proxy'] = proxy

    def process_response(self, request, response, spider):
        '''对返回的response处理'''
        # 如果返回的response状态不是200，重新生成当前request对象
        if response.status != 200:
            proxy = self.get_random_proxy()
            logging.info("this is response ip >> %s", proxy)
            # 对当前reque加上代理
            request.meta['proxy'] = proxy
            return request
        return response

    def get_random_proxy(self):
        '''随机取proxy'''
        proxy = random.choice(PROXY_IPS).strip()
        return proxy
